package com.youxue.teachin.set.child.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ali.music.navigator.backstack.Params;
import com.library.uiframework.fragment.BaseFragment;
import com.library.uiframework.utils.MToast;
import com.youxue.teachin.R;
import com.youxue.teachin.set.main.vo.KeyValueVO;
import com.youxue.teachin.set.repository.po.UserPO;
import com.youxue.teachin.user.userSystem.UserSystem;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-06 23:33
 * @version: 9.1.0
 */
public class ChangeTextFragment extends BaseFragment implements IChageUserView, OnClickListener {

    private View mRootView;
    private EditText mEditText;
    private ImageView mDelete;
    private TextView mRegistBt;

    UserChangePresener mPresenter;
    int type;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.set_change_text_fragment, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initView(View rootView) {
        mRootView = rootView;
        mEditText = (EditText)rootView.findViewById(R.id.edit_text);
        mDelete = (ImageView)rootView.findViewById(R.id.delete);
        mRegistBt = (TextView)rootView.findViewById(R.id.regist_bt);
        mRegistBt.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mEditText.setEnabled(true);
            }
        });

        Params params = getParams();
        type = params.getInt("type", 0);
        String value = params.getString("text", "");

        if (type == KeyValueVO.TYPE_NIKE_NAME) {
            mEditText.setHint("请输入昵称");
        } else {
            mEditText.setHint("用一句话介绍自己");
        }

        mEditText.setText(value);

        mPresenter = new UserChangePresener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        hideInputMethod();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideInputMethod();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.delete) {
            mEditText.setText("");
        } else {
            UserPO userPO = new UserPO();
            String textEdit = mEditText.getText().toString();
            if (type == KeyValueVO.TYPE_NIKE_NAME) {
                userPO.nickName = textEdit;
                userPO.realname = textEdit;
            } else {
                userPO.introduce = textEdit;
            }

            mPresenter.updateUser(userPO);
        }
    }

    @Override
    public void updateSucces() {
        if (type == KeyValueVO.TYPE_NIKE_NAME) {
            MToast.getInstance().setText("更新昵称成功");
            UserSystem.getInstance().getUserVO().nickname = mEditText.getText().toString();
        } else {
            UserSystem.getInstance().getUserVO().introduce = mEditText.getText().toString();
            MToast.getInstance().setText("更新介绍成功");
        }
        finish();
    }

    @Override
    public void updateFail() {
        if (type == KeyValueVO.TYPE_NIKE_NAME) {
            MToast.getInstance().setText("更新昵称失败");
        } else {
            MToast.getInstance().setText("更新介绍失败");
        }
    }
}
